<?php

namespace app\admin\services;

use think\App;
use think\facade\Request;
use think\Service;
use think\facade\Session;
use app\admin\model\StandmoneyModel;
use app\admin\model\OperationModel;
use think\facade\Config;
class StandardService extends Service
{
    public function __construct(Request $request = null, App $app = null)
    {
        parent::__construct($request, $app);
           $this->StandmoneyModel = new StandmoneyModel();
           $this->OperationModel = new OperationModel();
    }


    /**
     * @param 添加收费
     */
    public function addlist(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        $post = $post['data'];
        if(empty($post['title'])){
            return ['status'=>1, "msg"=>"请输入命名"];
        }
        if(empty($post['enable'])){
            return ['status'=>1, "msg"=>"请选择状态"];
        }
        if(empty($post['standard'])){
            return ['status'=>1, "msg"=>"请输入标准"];
        }
        if(empty($post['standard2'])){
            return ['status'=>1, "msg"=>"请输入标准2"];
        }
        if(empty($post['standard3'])){
            return ['status'=>1, "msg"=>"请输入标准3"];
        }
        if(empty($post['standard4'])){
            return ['status'=>1, "msg"=>"请输入标准4"];
        }
        $data = [
            'title'=> $post['title'],
            'enable'=> $post['enable'],
            'standard'=>$post['standard'],
            'standard2'=>$post['standard2'],
            'standard3'=>$post['standard3'],
            'standard4'=>$post['standard4']
        ];
        $rulset = $this->StandmoneyModel->polist($data);
        if(!$rulset){
            return ['status'=>1, "msg"=>"操作失败"];
        }
        $oper['actionInfo']= "添加收费";
        $uid = Session::get('uid','admin');
        $oper['actionTime'] = time();
        $oper['actionIp'] = getIp();
        $oper['uid'] = $uid;
        $oper['username'] = Session::get('username','admin');
        $alllog = $this->OperationModel->addlog($oper);
        if(!$alllog){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("USER_ADD");
    }


    /**
     * @param 获取收费标列表
     */
    public function datalist(){
        $post = Request::param();
        $page = isset($post['pageIndex'])?$post['pageIndex']:1;
        $limit = isset($post['pageSize'])?$post['pageSize']:10;
        if(!empty($post['title'])){
            $where = [['title','like','%'.$post['title'].'%']];
        }else{
            $where = [];
        }
        $list = $this->StandmoneyModel->problist($where,$page,$limit);
        $counts = $this->StandmoneyModel->county($where);
        $array = [];
        foreach ($list as $value){
            $value['stule'] = disa($value['enable']);
            $array[] = $value;
        }
        if(empty($list)){
            $data['rel'] = false;
            $data['msg'] = "暂无数据";
        }else{
            $data['rel'] = true;
            $data['msg'] = "获取成功";
        }
        $data['list'] = $array;
        $data['count'] = $counts;
        return $data;
    }


    /**
     * @param 获取当前修改收费标
     */
    public function editdata(){
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("NUKNOW_EORRE");
        }
        $id = $post['id'];
        $rulset = $this->StandmoneyModel->datalist($id);
        if(!$rulset){
            return $this->_getResponse("DATA_ERROR");
        }
        return $rulset;
    }


    /**
     * @param 修改平台域名
     */
    public function editlont(){
        $post = Request::param();
        if(empty($post['data']['title'])){
            return ['status'=>1, "msg"=>"请输入标准命名"];
        }
        if(empty($post['data']['enable'])){
            return ['status'=>1, "msg"=>"请选择状态"];
        }
        if(empty($post['data']['standard'])){
            return ['status'=>1, "msg"=>"请输入标准"];
        }
        if(empty($post['data']['standard2'])){
            return ['status'=>1, "msg"=>"请输入标准2"];
        }
        if(empty($post['data']['standard3'])){
            return ['status'=>1, "msg"=>"请输入标准3"];
        }
        if(empty($post['data']['standard4'])){
            return ['status'=>1, "msg"=>"请输入标准4"];
        }
        $data = [
            'title'=> $post['data']['title'],
            'enable'=> $post['data']['enable'],
            'standard'=>$post['data']['standard'],
            'standard2'=>$post['data']['standard2'],
            'standard3'=>$post['data']['standard3'],
            'standard4'=>$post['data']['standard4'],
            'updateTime'=>date('Y-m-d H:i:s',time())
        ];
        $where = [["id",'eq',$post['data']['titnameid']]];
        $rulest = $this->StandmoneyModel->edisont($where,$data);
        if(!$rulest){
            return ['status'=>1, "msg"=>"修改失败"];
        }
        $oper['actionInfo']= "修改平台";
        $uid = Session::get('uid','admin');
        $oper['actionTime'] = time();
        $oper['actionIp'] = getIp();
        $oper['uid'] = $uid;
        $oper['username'] = Session::get('username','admin');
        $alllog = $this->OperationModel->addlog($oper);
        if(!$alllog){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("EDIT_SUCCESS");
    }


    /**
     * @param 禁用、启用
     */
    public function stustlod(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $where = [['id','eq',$post['id']]];
        $rustl = $this->StandmoneyModel->exbut($where);
        if(!$rustl){
            return $this->_getResponse("NUKNOW_EORRE");
        }
        $state = $rustl['enable'];
        if($state==1){
            $data = ['enable'=> 2];
            $oper['actionInfo']= "关闭收费标准";
        }else{
            $data = ['enable'=> 1];
            $oper['actionInfo']= "启用收费标准";
        }
        $listos = $this->StandmoneyModel->eidtion($where,$data);
        if(!$listos){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        $uid = Session::get('uid','admin');
        $oper['actionTime'] = time();
        $oper['actionIp'] = getIp();
        $oper['uid'] = $uid;
        $oper['username'] = Session::get('username','admin');
        $alllog = $this->OperationModel->addlog($oper);
        if(!$alllog){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("ENBES_DATE");
    }


    /**
     * @param 删除收费标准
     */
    public function delete(){
        if(!Request::isPost()){
            return $this->_getResponse("NO_POST");
        }
        $post = Request::param();
        if(empty($post['id'])){
            return $this->_getResponse("PARAM_EORRE");
        }
        $id = $post['id'];
        $where = [['id','eq',$id]];
        $del = $this->StandmoneyModel->del($where);
        if(!$del){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        $oper['actionInfo']= "删除收费标准";
        $uid = Session::get('uid','admin');
        $oper['actionTime'] = time();
        $oper['actionIp'] = getIp();
        $oper['uid'] = $uid;
        $oper['username'] = Session::get('username','admin');
        $alllog = $this->OperationModel->addlog($oper);
        if(!$alllog){
            return $this->_getResponse("CAOZUO_ERROR");
        }
        return $this->_getResponse("DELETE_DATE");
    }


    /**
     * @param $key
     * @param array $data
     * @return mixed
     */
    protected function _getResponse($key, $data = [])
    {
        $maps = [
            "SUCCESS" => ["status" => 0, "msg" => "sccuess", "data" => $data],
            "USER_ADD" => ["status" => 0, "msg" => "添加成功", "data" => $data],
            "ENBES_DATE" => ["status" => 0, "msg" => "操作成功", "data" => $data],
            "DELETE_DATE" => ["status" => 0, "msg" => "删除成功", "data" => $data],
            "PARAM_EORRE" => ["status" => 1, "msg" => "参数错误", "data" => $data],
            "NUKNOW_EORRE" => ["status" => 2, "msg" => "未知错误", "data" => $data],
            "DATA_ERROR" => ["status" => 7, "msg" => "数据不存在", "data" => $data],
            "AUTH_EORRE" => ["status" => 3, "msg" => "权限不足", "data" => $data],
            "NO_POST" => ["status" => 4, "msg" => "非法请求", "data" => $data],
            "USER_ERROR" => ["status" => 5, "msg" => "用户名已存在", "data" => $data],
            "CAOZUO_ERROR" => ["status" => 6, "msg" => "操作失败", "data" => $data],
            "EDIT_SUCCESS" => ["status" => 0, "msg" => "修改成功", "data" => $data],
            "CONFIRM_SUCCESS" => ["status" => 0, "msg" => "确认成功", "data" => $data],
            "EMLIE_SUCCESS" => ["status" => 0, "msg" => "留言成功", "data" => $data]
        ];
        return $maps[$key];
    }
}